package com.fansl.allround.upms.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fansl.allround.common.core.constant.CacheConstants;
import com.fansl.allround.common.core.constant.CommonConstants;
import com.fansl.allround.common.core.util.R;
import com.fansl.allround.upms.api.entity.SysMenu;
import com.fansl.allround.upms.api.entity.SysRoleMenu;
import com.fansl.allround.upms.api.vo.MenuVO;
import com.fansl.allround.upms.mapper.SysMenuMapper;
import com.fansl.allround.upms.mapper.SysRoleMenuMapper;
import com.fansl.allround.upms.mapper.SysUserMenuMapper;
import com.fansl.allround.upms.service.SysMenuService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author fansl
 * @Description: 菜单权限表 服务实现类
 * @date 2019/8/13 15:24
 */
@Service
@AllArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    private final SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    @Cacheable(value = CacheConstants.CACHE_MENU_DETAIL, key = ("#roleId +'" + CacheConstants.CACHE_ROLE_MENU_SUFFIX + "'"))
    public List<MenuVO> getMenuByRoleId(Integer roleId) {
        return baseMapper.listMenusByRoleId(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = CacheConstants.CACHE_MENU_DETAIL, allEntries = true)
    public R removeMenuById(Integer id) {
        // 查询父节点为当前节点的节点
        List<SysMenu> menuList = this.list(Wrappers.<SysMenu>query()
                .lambda().eq(SysMenu::getParentId, id));
        if (CollUtil.isNotEmpty(menuList)) {
            return R.builder()
                    .code(CommonConstants.FAIL)
                    .msg("菜单含有下级不能删除").build();
        }

        sysRoleMenuMapper
                .delete(Wrappers.<SysRoleMenu>query()
                        .lambda().eq(SysRoleMenu::getMenuId, id));

        //删除当前菜单及其子菜单
        return new R(this.removeById(id));
    }

    @Override
    @CacheEvict(value = CacheConstants.CACHE_MENU_DETAIL, allEntries = true)
    public Boolean updateMenuById(SysMenu sysMenu) {
        return this.updateById(sysMenu);
    }

    /**
     * 获取用户的菜单
     *
     * @param userId
     * @return
     */
    @Override
    @Cacheable(value = CacheConstants.CACHE_MENU_DETAIL, key = "#userId+'" + CacheConstants.CACHE_USER_MENU_SUFFIX + "'")
    public List<MenuVO> getMenuByUserId(Integer userId) {
        return baseMapper.listMenusByUserId(userId);
    }

    /**
     * 获取用户权限
     *
     * @param userId
     * @return
     */
    @Override
    @Cacheable(value = CacheConstants.CACHE_MENU_DETAIL, key = "#userId+'" + CacheConstants.CACHE_USER_PERMISSION_SUFFIX + "'")
    public List<String> listPermissionByUserId(Integer userId) {
        return baseMapper.listPermissionsByUserId(userId).stream()
                .filter(permission -> StrUtil.isNotBlank(permission))
                .collect(Collectors.toList());
    }

    /**
     * 获取用户自定义菜单ID
     *
     * @param userId
     * @param type
     * @return
     */
    @Override
    public List<Integer> getUserMenuByType(Integer userId, Integer type) {
        return baseMapper.listMenusByType(userId, type);
    }
}
